package com.peng.leetcode.other;

import java.util.LinkedList;
import java.util.Queue;

/**
 * MergeKList
 *
 * @author: lupeng6
 * @create: 2020/11/14 23:31
 */
public class MergeKList {

    public static void main(String[] args) {
        ListNode l1 = new ListNode(1, new ListNode(4, new ListNode(5, null)));
        ListNode l2 = new ListNode(1, new ListNode(3, new ListNode(4, null)));
        ListNode l3 = new ListNode(2, new ListNode(6, null));

        ListNode listNode = new MergeKList().mergeKList(l1, l2, l3);

        String s= "";
        while (listNode != null) {
            s += "->" + listNode.val;
            listNode = listNode.next;
        }
        System.out.println(s);
    }


    public ListNode mergeKList(ListNode... lists) {
        Queue<ListNode> queue = new LinkedList<ListNode>();
        for (ListNode listNode : lists) {
            queue.add(listNode);
        }
        while(queue.size() > 1) {
            queue.add(new MergeTwoLists().mergeTwoLists2(queue.poll(), queue.poll()));
        }
        return queue.poll();
    }


}
